//
//  PersonalVipViewController.m
//  ChatForHot
//
//  Created by wr on 2018/10/11.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalVipViewController.h"
#import "PersonalVipViewModel.h"
#import "BaseTableView.h"
#import "PersonalVipCell.h"
#import "PersonalVipHeaderView.h"
#import "PersonalVipInfoModel.h"
#import "PurchaseManager.h"
#import "PersonalVipSectionFooter.h"
#import "PersonalHeaderVipCell.h"

@interface PersonalVipViewController () <UITableViewDelegate, UITableViewDataSource,PersonalVipHeaderViewDelegate>

@property (nonatomic, strong) PersonalVipViewModel *viewModel;
@property (nonatomic, strong) BaseTableView *HOTCHATtableView;
@property (nonatomic, strong) PersonalVipHeaderView *HOTCHATheadView;
@property (nonatomic, strong) UIButton *HOTCHATbuyVipBut;
@property (nonatomic, strong) NSString *productId;
@property (nonatomic, strong) PersonalVipInfoModel *vipModel;
@end

@implementation PersonalVipViewController

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
    
    self.viewModel = [[PersonalVipViewModel alloc] init];
}

- (void)HOTCHATInsertSubViews {
    [super HOTCHATInsertSubViews];
    
    self.HOTCHATtableView = [[BaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.HOTCHATtableView.delegate = self;
    self.HOTCHATtableView.dataSource = self;
    [self.HOTCHATtableView registerClass:[PersonalVipCell class] forCellReuseIdentifier:@"PersonalVipCell"];
    [self.HOTCHATtableView registerClass:[PersonalHeaderVipCell class] forCellReuseIdentifier:@"PersonalHeaderVipCell"];
    self.HOTCHATheadView = [[PersonalVipHeaderView alloc] initWithFrame:CGRectMake(0, 0, HOTCHATUIWIDTH, 225.0)];
    self.HOTCHATheadView.delegate = self;
    self.HOTCHATtableView.tableHeaderView = self.HOTCHATheadView;
    [self.view addSubview:self.HOTCHATtableView];
    
    self.HOTCHATbuyVipBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.HOTCHATbuyVipBut setBackgroundImage:[[UIImage imageNamed:@"personal_iden_sav_n"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateNormal];
    [self.HOTCHATbuyVipBut setBackgroundImage:[[UIImage imageNamed:@"personal_iden_sav_h"]resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateHighlighted];
    [self.HOTCHATbuyVipBut setTitle:NSLocalizedString(@"personalVIP2", nil) forState:UIControlStateNormal];
    [self.HOTCHATbuyVipBut setTitle:NSLocalizedString(@"personalVIP2", nil) forState:UIControlStateHighlighted];
    @weakify(self)
    [[self.HOTCHATbuyVipBut rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
       @strongify(self)
        [self HOTCHATstartLoading];
        [self HOTCHATpayActionExcute];
    }];
    [self.view addSubview:self.HOTCHATbuyVipBut];
}

- (void)HOTCHATsetNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
    [super HOTCHATsetNavigationItemsIsInEditMode:isInEditMode animated:animated];
    
    self.title = NSLocalizedString(@"person2", nil);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Restore" style:UIBarButtonItemStylePlain target:self action:@selector(HOTCHATrestore)];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.HOTCHATtableView.frame = self.view.bounds;
    self.HOTCHATtableView.height = self.view.bounds.size.height - 44 - 10 - 6;
    
    self.HOTCHATbuyVipBut.size = CGSizeMake(kScreenWidth - 30, 44);
    self.HOTCHATbuyVipBut.top = self.HOTCHATtableView.bottom + 6;
    self.HOTCHATbuyVipBut.left = 15;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    @weakify(self)
    [self.viewModel.requestVipInfoCommand.executionSignals subscribeNext:^(RACSignal *excution) {
        [[excution dematerialize] subscribeNext:^(id x) {
            @strongify(self)
            PersonalVipInfoModel *model = [self.viewModel.dataArray objectOrNilAtIndex:0];
            self.productId = model.productid;
            self.vipModel = model;
            [self.viewModel.requestCurrentUserVipInfoCommand execute:nil];
            [self.HOTCHATtableView reloadData];
        } error:^(NSError *error) {
            //错误ui处理
        }];
    }];
    
    [self.viewModel.requestCurrentUserVipInfoCommand.executionSignals subscribeNext:^(RACSignal *excution) {
        [[excution dematerialize] subscribeNext:^(id x) {
           @strongify(self)
            [self HOTCHATstopLoading];
            [self.HOTCHATheadView HOTCHATbind:self.viewModel];
            self.HOTCHATtableView.tableHeaderView = self.HOTCHATheadView;
        } error:^(NSError *error) {
            //错误ui处理
        }];
    }];
    
    [self HOTCHATstartLoading];
    [self.viewModel.requestVipInfoCommand execute:nil];
}

- (void)HOTCHATheaderViewCellSelected:(PersonalVipInfoModel *)model index:(NSInteger)index {
    self.productId = model.productid;
    self.vipModel = model;
}

- (void)HOTCHATpayActionExcute {
    @weakify(self)
    [[PurchaseManager shareInstance] startPurchWithID:self.productId model:self.vipModel completeHandle:^(HOTCHATPurchType type, NSData * _Nonnull data) {
        @strongify(self)
        [self.viewModel.requestCurrentUserVipInfoCommand execute:nil];
        [self HOTCHATstopLoading];
    }];
}

- (void)HOTCHATrestore {
    [self HOTCHATstartLoading];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self HOTCHATstopLoading];
        [self HOTCHATshowMessage:NSLocalizedString(@"personalVIP_10", nil)];
    });
    
}
#pragma mark - <UITableViewDataSource,UITableViewDelegate>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.viewModel.sectionOneCellDataSource.count;
    }else {
        return self.viewModel.sectionTwoCellDataSource.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        PersonalVipCellModel *row = [self.viewModel.sectionOneCellDataSource objectOrNilAtIndex:indexPath.row];
        return row.cellHeight;
    }else {
        PersonalVipCellModel *row = [self.viewModel.sectionTwoCellDataSource objectOrNilAtIndex:indexPath.row];
        return row.cellHeight;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        PersonalHeaderVipCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"PersonalHeaderVipCell"];
        PersonalVipCellModel *cellModel = [self.viewModel.sectionOneCellDataSource objectOrNilAtIndex:indexPath.row];
        [headerCell HOTCHATbind:cellModel];
        return headerCell;
    }else {
        PersonalVipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonalVipCell"];
        PersonalVipCellModel *cellModel = [self.viewModel.sectionTwoCellDataSource objectOrNilAtIndex:indexPath.row];
        [cell HOTCHATbind:cellModel];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
    }else {
        PersonalVipCellModel *cellModel = [self.viewModel.sectionTwoCellDataSource objectOrNilAtIndex:indexPath.row];
        if (cellModel.handler) { cellModel.handler(); }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        PersonalVipSectionFooter *footer = [[PersonalVipSectionFooter alloc] init];
        return footer;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 44;
    }else {
        return PixelOne;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return PixelOne;
}
@end
